package com.yhj.reggie.controller;

import com.yhj.reggie.common.R;
import com.yhj.reggie.entity.Category;
import com.yhj.reggie.entity.Page;
import com.yhj.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> saveCategory(@RequestBody Category category , HttpSession httpSession){
        Long id = (Long) httpSession.getAttribute("employee");
        category.setCreateUser(id);
        category.setCreateUser(id);

        categoryService.save(category);
        return R.success("添加成功");
    }
    
    @GetMapping("/page")
    public R<Page<Category>> findByPage(Integer page , Integer pageSize){

        Page<Category> resultPage = categoryService.findByPage(page, pageSize);

        return R.success(resultPage);
    }

    @DeleteMapping
    public R<String> delete (Long id){

        categoryService.delete(id);


        return R.success("删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category,HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        category.setUpdateUser(id);
        categoryService.update(category);
        return R.success("修改成功");
    }

    //获取指定类别的分类列表
    @RequestMapping("/list")
    public R<List<Category>> findAllByType(Integer type){
        List<Category> listByType = categoryService.findAllByType(type);
        return R.success(listByType);
    }

    public void test(){
        System.out.println("66666想不到啊");
    }


}
